package com.taojin.shoppinginterest.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.taojin.shoppinginterest.R;
import com.taojin.shoppinginterest.bean.End_Activity;

import java.util.HashMap;
import java.util.List;

/**
 * Created by T400 on 15-5-12.
 */
public class End_Activity_Adapter extends BaseAdapter {
    private Context context;
    private List<End_Activity> list;
    private BitmapUtils bitmapUtils;
    private HashMap<Integer, View> map = new HashMap<Integer, View>();

    public End_Activity_Adapter(Context context, List<End_Activity> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_welfarefragment_layout, null);
            viewHolder = new ViewHolder(convertView);
            map.put(position, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        getBitmap(viewHolder.img_logo, list.get(position).getLogo());
        getBitmap(viewHolder.img_url, list.get(position).getImg_url());

        viewHolder.shop_name.setText(list.get(position).getShop_name());
        viewHolder.shop_name.setText(list.get(position).getShop_name());
        viewHolder.cate.setText(list.get(position).getCate());
        viewHolder.name.setText(list.get(position).getName());
        viewHolder.title.setText(list.get(position).getTitle());
        viewHolder.time.setText(list.get(position).getTime());
        viewHolder.distance.setText(list.get(position).getDistance());

      //  convertView = map.get(position);
        return convertView;
    }

    public void getBitmap(ImageView img, String url) {
        BitmapDisplayConfig config = new BitmapDisplayConfig();
        config.setLoadFailedDrawable(context.getResources().getDrawable(R.mipmap.icon));
        config.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.icon));
        bitmapUtils.display(img, url, config, new BitmapLoadCallBack<ImageView>() {
            @Override
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                imageView.setImageBitmap(bitmap);
            }

            @Override
            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                imageView.setImageResource(R.mipmap.icon);
            }
        });
    }

    private static class ViewHolder {
        @ViewInject(R.id.img_url_item_welfarefragment)
        private ImageView img_url;
        @ViewInject(R.id.shop_name_item_welfarefragment)
        private TextView shop_name;
        @ViewInject(R.id.cate_item_welfarefragment)
        private TextView cate;

        @ViewInject(R.id.img_logo_item_welfarefragment)
        private ImageView img_logo;
        @ViewInject(R.id.name_item_welfarefragment)
        private TextView name;
        @ViewInject(R.id.title_item_welfarefragment)
        private TextView title;
        @ViewInject(R.id.time_item_welfarefragment)
        private TextView time;
        @ViewInject(R.id.distance_item_welfarefragment)
        private TextView distance;

        public ViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }


}
